package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jjwang on 4/17/14.
 */
public class Library {
    List<Book> books;
    List<Movie> movies;

    public Library() {
        initializeBookList();
        initializeMovieList();
    }

    private List<Movie> initializeMovieList() {
        this.movies = new ArrayList();
        movies.add(new Movie("Dehuan", "The Shawshank Redemption"));
        movies.add(new Movie("David","The Godfather"));
        movies.add(new Movie("Mata","Pulp Fiction"));
        movies.add(new Movie("Jiaji","Schindler's List"));
        movies.add(new Movie("Jordan","Fight Club"));
        return movies;

    }

    private List initializeBookList(){
        this.books = new ArrayList();
        books.add(new Book("Refactor"));
        books.add(new Book("Clean Code"));
        books.add(new Book("Spring MVC"));
        books.add(new Book("Thinking in Java"));
        books.add(new Book("Ruby on Rails"));
        return books;
    }

    public List<Book> getBooks() {
        return books;
    }

    public String getBooksPage(){
        String booksPage = "Book List\n";
        int index = 0;
        for(Book book:books) {
            if(!book.isAvailable()){
                continue;
            }
            index++;
            booksPage = booksPage + index + "." + book.getName() + "\n";
        }
        return booksPage;
    }

    public boolean checkoutBook(String bookName,User loaner) {
        return changeBookStatus(bookName,false,loaner);
    }

    public boolean returnBook(String bookName, User loaner) {
        return changeBookStatus(bookName,true,loaner);
    }

    private boolean changeBookStatus(String bookName,boolean status,User loaner) {
        for(Book book:books){
            if(book.getName().equals(bookName)&& book.isAvailable()!=status){
                book.setAvailable(status);
                if(!status){
                    book.setLoaner(loaner);
                }else {
                    if(!book.getLoaner().equals(loaner))
                        return false;
                    book.setLoaner(null);
                }
                return true;
            }
        }
        return false;
    }

    public boolean checkoutMovie(String movieName,User loaner) {
        return changeMovieStatus(movieName, false, loaner);
    }

    public boolean returnMovie(String movieName,User loaner) {
        return changeMovieStatus(movieName, true, loaner);
    }

    private boolean changeMovieStatus(String movieName,boolean status,User loaner) {
        for(Movie movie:movies){
            if(movie.getName().equals(movieName)&& movie.isAvailable()!=status){
                movie.setAvailable(status);
                if(!status){
                    movie.setLoaner(loaner);
                }else {
                    if(!movie.getLoaner().equals(loaner))
                        return false;
                    movie.setLoaner(null);
                }
                return true;
            }
        }
        return false;
    }

    public List<Movie> getMovies() {
        return movies;
    }

    public String getMoviesPage() {
        String moviesPage = "Movie List\n";
        int index = 0;
        for(Movie movie:movies) {
            if(!movie.isAvailable()){
                continue;
            }
            index++;
            moviesPage = moviesPage + index + "." + movie.getMovieInfo() + "\n";
        }
        return moviesPage;

    }
}
